<?php

function smarty_function_tool($params, &$smarty) {
	$request = Zend_Controller_Front::getInstance()->getRequest();
	$view = $smarty->getTemplateVars('this');
	$name = isset($params['name']) ? mb_strtolower($params['name']) : 'pencil';
	$params = $params + array(
		'class' =>  'tool tool-'.$name,
		'action' => $request->getActionName(),
		'controller' => $request->getControllerName(),
	);
	$acl = $view->navigation()->getAcl();
	$role = $view->navigation()->getRole();
	if ($acl && $role && (
		!$acl->hasRole($role) ||
		!$acl->has($params['controller']) ||
		!$acl->isAllowed($role, $params['controller'], $params['action'])
	))
	{
		return '';
	}
	if (!empty($params['title']))
	{
		$params['title'] = $view->translate($params['title']);
		$value = $params['title'].'&nbsp;';
	}
	else
	{
		$params['title'] = $view->translate('tool.'.$name);
		$value = '';
	}
	$href = $view->url(array_diff_key($params, array('name' => '', 'title' => '', 'class' => '')));
	return '<a href="'.$href.'" title="'.$params['title'].'" class="'.$params['class'].'">'.
		'<span class="ui-icon ui-icon-'.$name.'"></span>'.$value.'</a>';
}
